//
//  HomeViewModel.swift
//  Weibo
//
//  Created by mac on 16/1/16.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

import AFNetworking
import SVProgressHUD



class HomeViewModel: NSObject {

    
    
    func loadData(finished: ([StatusesModel]) -> ()) {
        
        let currentUser = UserCountViewModel()
        
        let urlString = "2/statuses/home_timeline.json"
        
        let paramater = ["access_token":currentUser.access_token]
        
        MKNetworkTools.shareNetworking.GET(urlString, parameters: paramater, progress: nil, success: { (_, responseObject) -> Void in
            
            guard let statues: [String : AnyObject] = responseObject as? [String : AnyObject] else {
                
                SVProgressHUD.showErrorWithStatus("statues++网络连接错误...")
                return
            }
            
            guard let dicts: [[String : AnyObject]] = statues["statuses"] as? [[String : AnyObject]] else {
                
                SVProgressHUD.showErrorWithStatus("dicts++网络连接错误...")
                return
            }
            
            //实例化 可变数组
            var models: [StatusesModel] = [StatusesModel]()
            for var item: [String : AnyObject] in  dicts {
                
                let model: StatusesModel = StatusesModel(dict: item)
                
                models.append(model)
            }
            
            finished(models)
//            print(models)
            
            }) { (_, error) -> Void in
                print("====")
                print(error)
        }
    }
    
}
